package com.gudaichaoren.spider.pageprocesser;

import cn.hutool.core.util.StrUtil;
import com.gudaichaoren.spider.constant.Constants;
import com.gudaichaoren.spider.entity.LotteriesShuangseAnalysis;
import com.gudaichaoren.spider.entity.LotteriesShuangseResults;
import com.gudaichaoren.spider.mapper.LotteriesShuangseResultsMapper;
import com.gudaichaoren.spider.pipeline.LotteriesShuangsePipeline;
import com.gudaichaoren.spider.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;
import us.codecraft.webmagic.utils.HttpConstant;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Component
public class LotteriesShuangsePageProcesser implements PageProcessor {

    @Autowired
    private LotteriesShuangsePipeline lotteriesShuangsePipeline;

    @Override
    public void process(Page page) {
        /**
         * 获取完整页面
         */
        String integralHtml = page.getHtml().toString();
        page.putField("integralHtml", integralHtml);

        /**
         * 获取中奖结果tr集合
         */
        List<Selectable> resultTrNodes = page.getHtml().xpath("//*[@id=\"tdata\"]/tr").nodes();
        ArrayList<LotteriesShuangseResults> lotteriesShuangseResultsList = new ArrayList<>();
        for (Selectable trNode : resultTrNodes) {
            if (StrUtil.equals("tdbck", trNode.xpath("//tr/@class").get())) {
                continue;
            }
            List<Selectable> tdNodes = trNode.xpath("//td").nodes();
            LotteriesShuangseDataSituation lotteriesShuangseDataSituation = new LotteriesShuangseDataSituation(new LotteriesShuangseResults());
            for (int i = 0; i < tdNodes.size(); i++) {
                for (BuildLotteriesShuangseDataEnum value : BuildLotteriesShuangseDataEnum.values()) {
                    if (i == value.getI()) {
                        value.handle(tdNodes.get(i), lotteriesShuangseDataSituation);
                    }
                }
            }
            lotteriesShuangseResultsList.add(lotteriesShuangseDataSituation.getLotteriesShuangseResults());
        }
        page.putField("lotteriesShuangseResultsList", lotteriesShuangseResultsList);

        /**获取分析结果tr集合
         *
         */
        List<Selectable> analysisTrNodes = page.getHtml().xpath("//*[@id=\"chartsTable\"]/tbody[3]/tr").nodes();
        ArrayList<LotteriesShuangseAnalysis> lotteriesShuangseAnalyseList = new ArrayList<>();
        for (Selectable analysisTrNode : analysisTrNodes) {
            if (StrUtil.equals("bd_bt_a", analysisTrNode.xpath("//tr/@class").get())) {
                continue;
            }
            LotteriesShuangseDataAnalysis lotteriesShuangseDataAnalysis = new LotteriesShuangseDataAnalysis(new LotteriesShuangseAnalysis());
            List<Selectable> tdNodes = analysisTrNode.xpath("//td").nodes();
            for (int i = 0; i < tdNodes.size(); i++) {
                for (BuildLotteriesShuangseDataEnum value : BuildLotteriesShuangseDataEnum.values()) {
                    if (i == value.getI()) {
                        value.handle(tdNodes.get(i), lotteriesShuangseDataAnalysis);
                    }
                }
            }
            lotteriesShuangseAnalyseList.add(lotteriesShuangseDataAnalysis.getLotteriesShuangseAnalysis());
        }
        page.putField("lotteriesShuangseAnalysisList", lotteriesShuangseAnalyseList);
    }

    /**
     * 号码:class:style
     *
     * @return
     */
    private String splicingBallInfo(Selectable selectable) {
        //获取号码
        String ballNumber = selectable.xpath("//td/text()").toString();
        String classType = selectable.xpath("//td/@class").get();
        String styleType = selectable.xpath("//td/@style").get();
        return StrUtil.join(":", ballNumber, classType, styleType);
    }

    @Override
    public Site getSite() {
        return Site.me().setRetryTimes(3).setSleepTime(100);
    }

    public void Crawling() {

        //设置POST请求
        Request request = new Request("https://zst.aicai.com/ssq?startIssue=&endIssue=&sIssue=&eIssue=&maxsize=100&openDate=&statisticsTag=1&sortTag=up");
//只有POST请求才可以添加附加参数
        request.setMethod(HttpConstant.Method.POST);

        Spider.create(new LotteriesShuangsePageProcesser()).addRequest(request).addPipeline(lotteriesShuangsePipeline).thread(1).run();
    }


}
